/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command.listener;

import cef.command.Command;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import commons.util.PrintUtil;
import genomemap.cef.command.RepeatsTests_Cmd;
import genomemap.test.RepeatsTransitionTests;

/**
 * @author Susanta Tewari
 * @since Sep 27, 2011
 */
public class RepeatsTests_PW extends CommandPrintWriter {

    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdOutputEvent)
            throws CommandOutputListenerException {

        final RepeatsTests_Cmd cmd = (RepeatsTests_Cmd) cmdOutputEvent.getSource();


        // independence
        final RepeatsTransitionTests.IndependenceTest ind_test = cmd.getIndependenceTest();

        getPrintWriter().println("Test of Independence: ");
        format_test_data(ind_test.getData(), ind_test.getChiSqValue(), ind_test.getDF(),
                         ind_test.getPValue());
        getPrintWriter().println("\n");


        // homogeneity
        final RepeatsTransitionTests.HomogeneityTest homo_test = cmd.getHomogeneityTest();

        getPrintWriter().println("Test of Homogeneity: ");
        format_test_data(homo_test.getData(), homo_test.getChiSqValue(), homo_test.getDF(),
                         homo_test.getPValue());


        // flush
        getPrintWriter().flush();
    }

    /**
     * @param counts
     * @param chiSquare
     * @param df
     * @param pValue
     */
    private void format_test_data(final int[][] counts, final double chiSquare, final int df,
                                  final double pValue) {

        final String[][] table = new String[counts.length + 1][4];

        table[0]    = new String[] { "", "S", "R", "A" };    // row header
        table[1][0] = "S";
        table[2][0] = "R";
        table[3][0] = "A";

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {
                table[i + 1][j + 1] = "" + counts[i][j];
            }
        }

        getPrintWriter().println(PrintUtil.print2D(table, 2, 1, null));
        getPrintWriter().println("Chi-Square: " + chiSquare);
        getPrintWriter().println("DF: " + df);
        getPrintWriter().println("PValue: " + pValue);
    }

    /**
     * @param counts
     * @param chiSquare
     * @param df
     * @param pValue
     */
    private void format_test_data(final double[][] counts, final double chiSquare, final int df,
                                  final double pValue) {

        final String[][] table = new String[counts.length + 1][4];

        table[0]    = new String[] { "", "S", "R", "A" };    // row header
        table[1][0] = "S";
        table[2][0] = "R";
        table[3][0] = "A";

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {
                table[i + 1][j + 1] = "" + counts[i][j];
            }
        }

        getPrintWriter().println(PrintUtil.print2D(table, 2, 1, null));
        getPrintWriter().println("Chi-Square: " + chiSquare);
        getPrintWriter().println("DF: " + df);
        getPrintWriter().println("PValue: " + pValue);
    }

    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return RepeatsTests_Cmd.class;
    }

    @Override
    public CommandOutputListener newInstance() {
        return new RepeatsTests_PW();
    }
}
